#include "Renderer.h"
#include "Camera.h"
#include "World.h"

World *world;

GLvoid Renderer::ReSizeGLScene(GLsizei width, GLsizei height) {
	if (height==0) {
		height=1;										// Making Height Equal One
	}

	glViewport(0,0,width,height);						// Reset The Current Viewport

	glMatrixMode(GL_PROJECTION);						// Select The Projection Matrix
	glLoadIdentity();									// Reset The Projection Matrix
	Camera::Initialize();

	// Calculate The Aspect Ratio Of The Window
	gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,1000.0f);
	glTranslatef(Camera::position.x, 0.0f, -50.0f);
	glMatrixMode(GL_MODELVIEW);							// Select The Modelview Matrix
	glLoadIdentity();									// Reset The Modelview Matrix
}

int Renderer::InitGL(GLvoid) {
	glShadeModel(GL_SMOOTH);							// Enable Smooth Shading
	glClearColor(0.0f, 0.0f, 0.0f, 0.5f);				// Black Background
	glClearDepth(1.0f);									// Depth Buffer Setup
	glEnable(GL_DEPTH_TEST);							// Enables Depth Testing
	glDepthFunc(GL_LEQUAL);								// The Type Of Depth Testing To Do
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);	// Really Nice Perspective Calculations
	
	//Create world
	world = new World();
	world->Initialize();
	
	return TRUE;
}

GLvoid Renderer::BeginRender(GLvoid) {
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	// Clear Screen And Depth Buffer
	glMatrixMode(GL_MODELVIEW);
}

int Renderer::Draw(GLvoid) {
	BeginRender();
	glLoadIdentity();

	if(GetAsyncKeyState(0x45)) {
		Camera::ForwardBackward(-1);
	}
	if(GetAsyncKeyState(0x51)) {
		Camera::ForwardBackward(1);
	}

	if(GetAsyncKeyState(0x57)) {
		Camera::Vertical(-1);
	}
	if(GetAsyncKeyState(0x53)) {
		Camera::Vertical(1);
	}

	if(GetAsyncKeyState(0x41)) {
		Camera::Lateral(1);
	}
	if(GetAsyncKeyState(0x44)) {
		Camera::Lateral(-1);
	}

	if(KEY_DOWN(VK_UP)) {
		Camera::Pitch(-1);
	}

	if(KEY_DOWN(VK_DOWN)) {
		Camera::Pitch(1);
	}

	if(KEY_DOWN(VK_LEFT)) {
		Camera::Yaw(-1);
	}
	if(KEY_DOWN(VK_RIGHT)) {
		Camera::Yaw(1);
	}

	// Do draw here
	world->Draw();

	return TRUE;
}